﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;


namespace team33_ass3
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class Camera : Microsoft.Xna.Framework.GameComponent
    {
        public Matrix view { get; protected set; }
        public Matrix projection { get; protected set; }
        public Vector3 position { get; protected set; }
        Vector3 up;
        Vector3 thirdPersonReference;
        private Player player;




        public Camera(Game game, Viewport viewport, Vector3 pos, Vector3 target, Vector3 up, Player player)
            : base(game)
        {
            // Build camera view matrix
            this.up = up;

            thirdPersonReference = new Vector3(0, 350, -200);
            this.player = player;

            CreateLookAt();


            projection = Matrix.CreatePerspectiveFieldOfView(
                MathHelper.PiOver4,
                (float)viewport.Width /
                (float)viewport.Height,
                1, 2000);

        }

        private void CreateLookAt()
        {
            Matrix rotationMatrix = Matrix.CreateRotationY(player.yaw);

            // Create a vector pointing the direction the camera is facing
            Vector3 transformedReference = Vector3.Transform(thirdPersonReference, rotationMatrix);

            // Calculate the position the camera is looking from
            position = transformedReference + player.position;

            // Setup the view matrix
            view = Matrix.CreateLookAt(position, player.position, up);
        }

        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public override void Initialize()
        {


            base.Initialize();
        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            UpdateCamera(gameTime);
            base.Update(gameTime);
        }



        private void UpdateCamera(GameTime gameTime)
        {
            CreateLookAt();
        }
    }
}
